$LOAD_PATH << "../lib"

require "starruby"
require "starframe"

StarFrame::Image.load

# 点滅する星
class StarSprite < StarFrame::Sprite
  @texture = StarFrame::Image::Star
  @center_x = 8
  @center_y = 8
  
  include StarFrame::Sprite::Animatable
  # 点滅アニメーションを定義
  animation :blink do
    # 「0x00と0xFFdで」「2フレームごとに」ブロックを繰り返す。
    frames [0x00, 0xFF], 2 do |c|
      self.alpha = c
    end
  end
  
  def init *args
    @movement_x = 2
    @movement_y = 4
    @angular_velocity = 3.degree
  end
  
  def update
    refrect_v = @x > 312 || @x < 8
    refrect_h = @y > 232 || @y < 8
    @movement_x *= -1       if refrect_v
    @movement_y *= -1       if refrect_h
    @angular_velocity *= -1 if refrect_v || refrect_h
  end
end
# アニメーションする音符
class AnimateMusicSprite < StarFrame::Sprite
  @texture = StarFrame::Image::Music
  @center_x = 8
  @center_y = 8
  
  include StarFrame::Sprite::Animatable
  # 回転アニメーションを定義
  animation :roll do
    # 40フレームで角度を変更
    frames 40.linear do |c|
      self.angle = (c*9).degree
    end
  end
  # 移動アニメーションを定義
  animation :move, :loop => false do
    # 並列に
    parallel do
      # 160フレーム（加速）で右に移動
      frames 160.accelerate do |c|
        self.x = c*2
      end
      # 160フレーム（減速）で下に移動
      frames 160.decelerate do |c|
        self.y = c*1.5
      end
    end
  end
  
  def init *args
    @movement_x = 0
    @movement_y = 0
  end
  
  def update
    animation(:move).restart if StarRuby::Input.keys(:keyboard).include? :space
  end
end

# 背景
class BackgroundSprite < StarFrame::Sprite
  @texture = StarFrame::Image::Back3x1
  
  include StarFrame::Sprite::Animatable
  # アニメーションを定義
  animation :animation do
    # 20フレームごとに3コマを繰り返す
    frames [0, 640, 1280], 20 do |c|
      self.src_x = c
    end
  end
  
  def init *args
    self.scale_x = 0.5
    self.scale_y = 0.5
    
    self.src_x = 0
    self.src_width = 640
  end
end

# タイトル
class MainScene < StarFrame::Scene
  def init *args
    # @fps = 60
    @background = BackgroundSprite.new(0, 0)
    @star = StarSprite.new(16, 16)
    @animation_star = AnimateMusicSprite.new(0, 0)
    
    $window    ||= SubWindow.new(160, 120)
    $playscene ||= PlayScene.new(@game)
    
    @duration = 0
  end
  
  def update
    @background.call
    @star.call
    @animation_star.call
    $window.call
    
    if StarRuby::Input.keys(:keyboard).include? :escape
      @duration += 1
      exit_scene if @duration == 60
    else
      @duration = 0
    end
    if StarRuby::Input.keys(:keyboard).include? :enter
      next_scene $playscene
    end
  end
  
  def render
    @background.render_to @screen
    @star.render_to @screen
    @animation_star.render_to @screen
    $window.render_to @screen
  end
end

# ゲーム
class PlayScene < StarFrame::Scene
  def init *args
    @image = StarFrame::Image::Music
    @bg = StarFrame::Image::FallingBlocks::Background
    @x = 160-8+4
    @y = 0
  end
  
  def update
    $window.call
    
    @x -= 4 if StarRuby::Input.keys(:keyboard).include? :left
    @x += 4 if StarRuby::Input.keys(:keyboard).include? :right
    @y -= 4 if StarRuby::Input.keys(:keyboard).include? :up
    @y += 4 if StarRuby::Input.keys(:keyboard).include? :down
    
    if StarRuby::Input.keys(:keyboard).include? :escape
      next_scene MainScene.new(@game)
    end
  end
  
  def render
    options = {
      :camera_x => @bg.width + @x,
      :camera_y => @bg.height + @y,
      :camera_height => 16,
      :intersection_x => 160,
      :intersection_y => 128,
      :loop => true,
      :blur => :background,
    }
    
    @screen.render_in_perspective @bg, options
    render_texture @image, (@x+320)%320, (@y+240)%240
    $window.render_to @screen
  end
end

# サブウィンドウ
class SubWindow < StarFrame::Sprite
  @texture = StarFrame::Image::Sound
  @center_x = 8
  @center_y = 64
  
  attr_accessor :screen
  
  def init *args
    @angular_velocity = 1.degree
  end
  
  def update
    @angular_velocity -= 1.degrees if StarRuby::Input.keys(:keyboard).include? :left
    @angular_velocity += 1.degrees if StarRuby::Input.keys(:keyboard).include? :right
  end
end

StarFrame.start :width => 320, :height => 240,
                :title => "Sample", :fps => 60, :fullscreen => false
